// pages/all_shop_list/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    one_class_list: [],
    shop_list:[],
    isOne_active: 0,
    istwo_active: 0,
    isthree_active: 0,
    childId: 0,
    left:0,
    search_val: '搜索商品',
    cart_num: 0,
    tabBar: app.globalData.tabBar,
    bgIsshow: false,
    spece_show:true,
    name_show:false,
    brand_show:false,
    animationData: {},
    down:{},
    cate_id:0,
    shop_list_scroll:0,
    two_left:0,
    three_left:0,
    screen_list: [
      { name: '只看热销', value:"is_hot"},
      { name: '销量', value:"sales"},
      { name: '筛选'}
    ],
    choose_obj:{name:'筛选',checked:false},
    brand_list:[],
    stag_list:[],
    balls: [
      { isShow: false, animationData: null },
      { isShow: false, animationData: null }
    ],
    order_num:0
  },
  oneActive: function (e) {
    var width = e.currentTarget.offsetLeft / e.currentTarget.id
    var that = this
    var choose_obj = this.data.choose_obj
    choose_obj.checked = false
    this.setData({
      isOne_active: e.currentTarget.id,
      left: e.currentTarget.offsetLeft - width * 2,
      istwo_active:0,
      choose_obj: choose_obj,
      three_left:0
    })
    wx.showLoading({
      title: '加载中',
    })
    var cate_id = e.currentTarget.dataset.value
    that.setData({
      ismany: that.data.one_class_list[that.data.isOne_active]._child[0]._child.length > 8
    })
    if (that.data.one_class_list[that.data.isOne_active]._child[0]._child.length > 8){
      cate_id = that.data.one_class_list[that.data.isOne_active]._child[0]._child[0].cate_id
    }
    app.ajax({
      url: app.host + 'mall/goods?cate_id=' + cate_id,
      success: function (res) {
        var shop_list = that.shop_list(res)
        var title_list = []
        for (let i in shop_list) {
          if (shop_list[i].is_title) {
            title_list.push(shop_list[i])
          }
        }
        that.title_list = title_list
        wx.hideLoading()
        that.setData({
          shop_list_scroll: 0,
          shop_list: shop_list,
          cate_id: cate_id,
          isthree_active: shop_list[0].cate_id
        })
      }
    })
    app.ajax({
      url: app.host + "mall/brand/catebrand?cate_id=" + e.currentTarget.dataset.value,
      success:function(res){
        that.setData({
          brand_list:res.data.data
        })
      }
    })
    app.ajax({
      url: app.host + "mall/goodstag?cate_id=" + e.currentTarget.dataset.value,
      success: function (res) {
        that.setData({
          stag_list: res.data.data
        })
      }
    }) 
  },
  twoActive: function (e) {
    var that = this
    var width = e.currentTarget.offsetLeft / e.currentTarget.dataset.id
    var choose_obj = this.data.choose_obj
    choose_obj.checked = false
    this.setData({
      istwo_active: e.currentTarget.dataset.id,
      choose_obj: choose_obj,
      two_left: e.currentTarget.offsetLeft - width * 2,
      three_left:0
    })
    wx.showLoading({
      title: '加载中',
    })
    var cate_id = e.currentTarget.dataset.value
    that.setData({
      ismany: that.data.one_class_list[that.data.isOne_active]._child[that.data.istwo_active]._child.length > 8
    })
    if (that.data.one_class_list[that.data.isOne_active]._child[that.data.istwo_active]._child.length > 8) {
      cate_id = that.data.one_class_list[that.data.isOne_active]._child[that.data.istwo_active]._child[0].cate_id
    }
    app.ajax({
      url: app.host + 'mall/goods?cate_id=' + cate_id,
      success:function(res){
       var shop_list = that.shop_list(res)
       var title_list = []
       for (let i in shop_list) {
         if (shop_list[i].is_title) {
           title_list.push(shop_list[i])
         }
       }
       that.title_list = title_list
       wx.hideLoading()
        that.setData({
          shop_list_scroll:0,
          isthree_active: shop_list[0].cate_id,
          shop_list: shop_list,
          cate_id: cate_id
        })
      }
    })
    app.ajax({
      url: app.host + "mall/brand/catebrand?cate_id=" + e.currentTarget.dataset.value,
      success: function (res) {
        that.setData({
          brand_list: res.data.data
        })
      }
    })
    app.ajax({
      url: app.host + "mall/goodstag?cate_id=" + e.currentTarget.dataset.value,
      success: function (res) {
        that.setData({
          stag_list: res.data.data
        })
      }
    })    
  },
  threeActive:function(e){
    var that = this
    var index = e.currentTarget.dataset.id
    var width = e.currentTarget.offsetLeft / index    
    var id = e.currentTarget.id
    this.setData({
      isthree_active: id,
      three_left: e.currentTarget.offsetLeft - width * 2,
    })
    if(that.data.ismany){
      wx.showLoading({
        title: '加载中',
      })
      app.ajax({
        url: app.host + 'mall/goods?cate_id=' + id,
        success: function (res) {
          var shop_list = that.shop_list(res)
          var title_list = []
          for (let i in shop_list) {
            if (shop_list[i].is_title) {
              title_list.push(shop_list[i])
            }
          }
          that.title_list = title_list
          wx.hideLoading()
          that.setData({
            shop_list_scroll: 0,
            shop_list: shop_list,
            cate_id: id
          })
        }
      })
    }else{
      wx.createSelectorQuery().select('.allshop_list').scrollOffset(function(res){
        var pagescrollTop = res.scrollTop  // 节点的竖直滚动位置
        wx.createSelectorQuery().select('#title_' + id).boundingClientRect(function(rect){
            that.setData({
              shop_list_scroll: rect.top + pagescrollTop - that.top_num_height,
            })
        }).exec()
      }).exec()
    }
  },
  screen: function (e) {
    var index = e.currentTarget.id
    var list = this.data.screen_list
    var that = this
    var value = 0
    var obj = {}
    if(index!=2){
      wx.showLoading({
        title: '加载中',
      })
    }
    obj['cate_id'] = this.data.cate_id
    if (index == 2) {
      this.animation.right(0).step()
      this.setData({
        animationData: this.animation.export(),
        bgIsshow: true
      })
      return
    }else{
      app.arrset({
        that: this,
        str: "screen_list[" + index + "].checked",
        checkObj: this.data.screen_list[index].checked
      })
    }
    
    for (var i in list){
      if(list[i].checked){
        value = list[i].value
        obj[value] = 1
      }
    }
    app.ajax({
      url: app.host + "mall/goods",
      data: obj,
      success: function (res) {
        var shop_list = that.shop_list(res)
        wx.hideLoading()
        that.setData({
          shop_list: shop_list
        })
      }
    })
  },
  hide: function () {
    var that = this
    var cate_id = that.data.cate_id
    var brand = []
    var label_name = []
    var brand_list = this.data.brand_list
    var stag_list = this.data.stag_list
    for (let i in brand_list) {
      if (brand_list[i].checked) {
        brand.push(brand_list[i].brand_id)
      }
    }
    for (let j in stag_list) {
      if (stag_list[j].checked) {
        label_name.push(stag_list[j].label_name)
      }
    }
    app.ajax({
      url: app.host + 'mall/goods',
      data: {
        cate_id: cate_id,
        brand: brand.join(','),
        name: label_name.join(',')
      },
      success:function(msg){
        var shop_list = that.shop_list(msg)
        that.setData({
          shop_list: shop_list
        })
      }
    })
    this.animation.right("-75%").step()
    this.setData({
      animationData: this.animation.export(),
      bgIsshow: false
    })
    this.setData({
      "choose_obj.checked": false
    })
    for (let i in this.data.brand_list) {
      if (this.data.brand_list[i].checked){
        this.setData({
          "choose_obj.checked": true
        })
      }         
    }
    for (let j in this.data.stag_list) {
      if (this.data.stag_list[j].checked) {
        this.setData({
          "choose_obj.checked": true
        })
      }
    }
  },
  brand_check:function(e){
    var brand_id = e.currentTarget.dataset.brand_id
    var brand_list = this.data.brand_list
    for (var i in brand_list){
      if(brand_list[i].brand_id == brand_id){
        if (brand_list[i].checked){
          brand_list[i].checked = false
        }else{
          brand_list[i].checked = true
        }
      }
    }
    this.setData({
      brand_list: brand_list
    })
  },
  stag_check:function(e){
    var label_id = e.currentTarget.dataset.label_id
    var stag_list = this.data.stag_list
    for (var i in stag_list) {
      if (stag_list[i].label_id == label_id) {
        if (stag_list[i].checked) {
          stag_list[i].checked = false
        } else {
          stag_list[i].checked = true
        }
      }
    }
    this.setData({
      stag_list: stag_list
    })
  },
  reset: function () {
    var brand_list = this.data.brand_list
    var stag_list = this.data.stag_list
    for (let i in brand_list) {
      brand_list[i].checked = false;
    }
    for (let j in stag_list){
      stag_list[j].checked = false;
    }
    this.setData({
      stag_list: stag_list,
      brand_list: brand_list
    })
  },
  choose_spece:function(e){
    var index = e.currentTarget.dataset.index
    var shop_list = this.data.shop_list
    if (shop_list[index].show_spece) {
      shop_list[index].show_spece = false
      shop_list[index].choose_text = "收起"
    } else {
      shop_list[index].show_spece = true
      shop_list[index].choose_text = "选规格"
    }
    this.setData({
      shop_list: shop_list
    })
  },
  brand_down:function(){
    var brand_show = this.data.brand_show
    this.setData({
      brand_show: !brand_show
    })
  },
  name_down:function(){
    var name_show = this.data.name_show
    this.setData({
      name_show: !name_show
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear'
    })
    wx.showLoading({
      title:'加载中'
    })
    app.ajax({
      url:app.host + 'mall/goodscate',
      success:function(res){
        var one_class_list = res.data.data
        that.setData({
          one_class_list: one_class_list,
          ismany: one_class_list[0]._child[0]._child.length > 8
        })
        if(that.data.ismany){
          app.ajax({
            url: app.host + 'mall/goods?cate_id=' + that.data.one_class_list[0]._child[0]._child[0].cate_id,
            success: function (res) {
              var shop_list = that.shop_list(res)
              wx.hideLoading()
              that.setData({
                shop_list: shop_list,
                cate_id: one_class_list[0]._child[0]._child[0].cate_id,
                isthree_active: shop_list[0].cate_id
              })
            }
          })
        }else{
          app.ajax({
            url: app.host + 'mall/goods?cate_id=' + that.data.one_class_list[0]._child[0].cate_id,
            success: function (res) {
              var shop_list = that.shop_list(res)
              var title_list = []
              for(let i in shop_list){
                if (shop_list[i].is_title){
                  title_list.push(shop_list[i])
                }
              }
              that.title_list = title_list
              wx.hideLoading()
              that.setData({
                shop_list: shop_list,
                cate_id: one_class_list[0]._child[0].cate_id,
                isthree_active: shop_list[0].cate_id
              })
            }
          })
        }
        app.ajax({
          url: app.host + "mall/brand/catebrand?cate_id=" + that.data.one_class_list[0]._child[0].cate_id,
          success: function (res) {
            that.setData({
              brand_list: res.data.data
            })
          }
        })
        app.ajax({
          url: app.host + "mall/goodstag?cate_id=" + that.data.one_class_list[0]._child[0].cate_id,
          success: function (res) {
            that.setData({
              stag_list: res.data.data
            })
          }
        })
      }
    })  
    app.ajax({
      url: app.host + "mall/cart",
      success: function (res) {
        var data = res.data.data
        var cart_num = 0
        for (var i in data) {
          cart_num = data[i].num * data.length
        }
        that.setData({
          cart_num: cart_num
        })
      }
    })
    wx.createSelectorQuery().select('.top_num').boundingClientRect(function(rect){
      that.top_num_height = rect.height
    }).exec()
    wx.createSelectorQuery().select('.two_class').boundingClientRect(function (rect) {
      that.two_class_width = rect.width
    }).exec()
    wx.createSelectorQuery().selectViewport().boundingClientRect(function(rect){
      that.page_height = rect.height
    }).exec()
  },
  shop_list:function(res){
    var shop_list = res.data.data
    for (var i in shop_list) {
      shop_list[i].index = i
      shop_list[i].show_spece = true
      shop_list[i].choose_text = "选规格"
    }
    return shop_list
  },
  add_cart:function(e){
    app.ball_animate(e, this)
    var that = this 
    var specval_id = e.currentTarget.dataset.specval_id
    var shop_list = this.data.shop_list
    for (var i in shop_list ){
      for(var j in shop_list[i].list){
        if (shop_list[i].list[j].specval_id == specval_id){
          shop_list[i].list[j].loading = true
          break
        }
      }
    }
    this.setData({
      shop_list: shop_list
    })
    app.add_cart(e,this,function(){
      var shop_list = that.data.shop_list
      for (var i in shop_list) {
        for (var j in shop_list[i].list) {
          if (shop_list[i].list[j].specval_id == specval_id) {
            shop_list[i].list[j].loading = false
            break
          }
        }
      }
      that.setData({
        shop_list: shop_list
      })
    })
  },
  allshop_list_scroll:function(e){
    var that = this
    var scrollTop = e.detail.scrollTop
    // var shop_list = this.data.shop_list
    var three_left = that.data.three_left || 0
    var title_list = that.title_list
    for (let i in title_list){
      wx.createSelectorQuery().select('#title_' + title_list[i].cate_id).boundingClientRect(function(rect){
          if (rect.top >= 0 && rect.top <= that.top_num_height){
            wx.createSelectorQuery().select('.bt_' + title_list[i].cate_id).boundingClientRect(function(res){
              var offsetleft = res.left + three_left
              var width = offsetleft / res.dataset.id       
              that.setData({
                isthree_active: title_list[i].cate_id,
                three_left: offsetleft - width * 2
              })
            }).exec()
          }
          if (rect.top > that.page_height && rect.top < that.page_height + 150){
            wx.createSelectorQuery().select('.bt_' + title_list[i-1].cate_id).boundingClientRect(function (res) {
              var offsetleft = res.left + three_left
              var width = offsetleft / res.dataset.id
              that.setData({
                isthree_active: title_list[i-1].cate_id,
                three_left: offsetleft - width * 2
              })
            }).exec()
          }
        }).exec()
    }
  },
  add_freq: function (e) {
    app.ajax({
      url: app.host + 'mall/Freqlist/add_freq',
      data: {
        goods_id: e.currentTarget.dataset.goods_id
      },
      success: function (res) {
          wx.showToast({
            title: res.data.msg,
          })
      },
    });
  },
 
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.cart_num(this)
    app.new_order(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})